<?php


namespace common\server;

use yii\base\BaseObject;

/**
 * Class Server
 *
 * @package common\server
 */
abstract class Server extends BaseObject
{

    static $models = [];

    private static $error;

    public static function setError($msg){
        self::$error = $msg;
    }


    public static function getError(){
        return self::$error;
    }

    /**
     * 获取单实例
     * @return static
     */
    static public function instance(){
        $name =  get_called_class();
        if( !isset( self::$models[$name] ) ){
            self::$models[$name] = new $name();
        }
        return self::$models[$name];
    }


    public function transaction($call)
    {
        $transaction = \Yii::$app->db->beginTransaction();
        try {
            $rs = $call();
            if (empty($rs)) {
                $transaction->rollBack();
            }else {
                $transaction->commit();
            }
            return $rs;
        } catch (\Throwable $ex) {
            $transaction->rollBack();
        }

        return false;
    }
}